package com.flyco.modules.sms.service.impl;

import com.flyco.modules.sms.entity.SmsInvitationInfo;
import com.flyco.modules.sms.entity.SmsInvitationAttribute;
import com.flyco.modules.sms.entity.SmsInvitationApply;
import com.flyco.modules.sms.mapper.SmsInvitationAttributeMapper;
import com.flyco.modules.sms.mapper.SmsInvitationApplyMapper;
import com.flyco.modules.sms.mapper.SmsInvitationInfoMapper;
import com.flyco.modules.sms.service.ISmsInvitationInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 众测活动管理
 * @Author: flyco
 * @Date:   2020-03-10
 * @Version: V1.0
 */
@Service
public class SmsInvitationInfoServiceImpl extends ServiceImpl<SmsInvitationInfoMapper, SmsInvitationInfo> implements ISmsInvitationInfoService {

	@Autowired
	private SmsInvitationInfoMapper smsInvitationInfoMapper;
	@Autowired
	private SmsInvitationAttributeMapper smsInvitationAttributeMapper;
	@Autowired
	private SmsInvitationApplyMapper smsInvitationApplyMapper;
	
	@Override
	@Transactional
	public void saveMain(SmsInvitationInfo smsInvitationInfo, List<SmsInvitationAttribute> smsInvitationAttributeList,List<SmsInvitationApply> smsInvitationApplyList) {
		smsInvitationInfoMapper.insert(smsInvitationInfo);
		for(SmsInvitationAttribute entity:smsInvitationAttributeList) {
			//外键设置
			entity.setInfoId(smsInvitationInfo.getId());
			smsInvitationAttributeMapper.insert(entity);
		}
		for(SmsInvitationApply entity:smsInvitationApplyList) {
			//外键设置
			entity.setInfoId(smsInvitationInfo.getId());
			smsInvitationApplyMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(SmsInvitationInfo smsInvitationInfo,List<SmsInvitationAttribute> smsInvitationAttributeList,List<SmsInvitationApply> smsInvitationApplyList) {
		smsInvitationInfoMapper.updateById(smsInvitationInfo);
		
		//1.先删除子表数据
		smsInvitationAttributeMapper.deleteByMainId(smsInvitationInfo.getId());
		smsInvitationApplyMapper.deleteByMainId(smsInvitationInfo.getId());
		
		//2.子表数据重新插入
		for(SmsInvitationAttribute entity:smsInvitationAttributeList) {
			//外键设置
			entity.setInfoId(smsInvitationInfo.getId());
			smsInvitationAttributeMapper.insert(entity);
		}
		for(SmsInvitationApply entity:smsInvitationApplyList) {
			//外键设置
			entity.setInfoId(smsInvitationInfo.getId());
			smsInvitationApplyMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		smsInvitationAttributeMapper.deleteByMainId(id);
		smsInvitationApplyMapper.deleteByMainId(id);
		smsInvitationInfoMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			smsInvitationAttributeMapper.deleteByMainId(Long.valueOf(id.toString()));
			smsInvitationApplyMapper.deleteByMainId(Long.valueOf(id.toString()));
			smsInvitationInfoMapper.deleteById(id);
		}
	}
	
}
